<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Show
 *
 * @property int $id
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $name_en
 * @property string $name_zh
 * @property string $detail
 * @property string $key_word
 * @property string $description
 * @property string $type
 * @property string $director
 * @property string $play_writer
 * @property string $actor
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Article[] $articles
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Episode[] $episodes
 * @property-read mixed $article_list
 * @property-read mixed $coverage_image
 * @property-read mixed $coverage_square
 * @property-read mixed $coverage_url
 * @property-read mixed $detail_image_url
 * @property-read mixed $ep_add
 * @property-read mixed $ep_list
 * @property-read mixed $episodes_list
 * @property-read mixed $excerpt
 * @property-read mixed $eztv_url
 * @property-read mixed $full_name
 * @property-read mixed $image_url
 * @property-read mixed $latest_episode
 * @property-read mixed $latest_episodes
 * @property-read mixed $score
 * @property-read mixed $show_image
 * @property-read mixed $site_url
 * @property-read mixed $ttmj_url
 * @property-read mixed $url_list
 * @property-read mixed $yyets_url
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Image[] $images
 * @property-read \App\Vote $vote
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereActor($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereDescription($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereDetail($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereDirector($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereKeyWord($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereNameEn($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereNameZh($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show wherePlayWriter($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereType($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string $alias 美剧其他中文名称,逗号分隔
 * @method static \Illuminate\Database\Query\Builder|\App\Show whereAlias($value)
 */
class Show extends Model
{
    protected $fillable = [
        'name_en',
        'name_zh',
        'detail',
        'type',
        'director',
        'play_writer',
        'actor'
    ];

    protected $key = 'default-150-200.png';
    
    public function setNameZhAttribute($value) {
        if(strlen($value)>1) {
            $this->attributes['name_zh'] = $value;
        }
    }
    public function setTypeAttribute($value) {
        $this->attributes['type'] = trim(str_replace('类型：','',$value));
    }
    public function setDetailAttribute($value) {
        $this->attributes['detail'] = strip_tags($value, '<hr><param><div><pre><ul><li><ol><table><td><tr><embed><video><object><tbody><th><thead><p><br><hr><h1><h2><h3><h4><h5><span><strong><i>');
    }
    public function setPlayWriterAttribute($value) {
        $this->attributes['play_writer'] = trim(str_replace('編劇：','',$value));
    }
    public function setDirectorAttribute($value) {
        $this->attributes['director'] = trim(str_replace('导演：','',$value));
    }
    public function setKeyWordAttribute($value) {
        $this->attributes['key_word'] = trim(str_replace('别名：','',$value));
    }
    public function setActorAttribute($value) {
        $this->attributes['actor'] = trim(str_replace('主演：','',$value));
    }
    public function vote() {
        return $this->hasOne('App\Vote');
    }
    public function getFullNameAttribute() {
        return $this->name_en.$this->name_zh;
    }
    public function episodes()
    {
        return $this->hasMany('App\Episode');
    }
    public function images()
    {
        return $this->belongsToMany('App\Image');
    }
    public function articles() {
        return $this->belongsToMany('App\Article');
    }

    public function getArticleListAttribute() {
        return $this->articles()->pluck('id')->toArray();
    }

    public function getUrlListAttribute() {
        //dd($this->images->toArray());
        return $this->images()->pluck('fname');
    }
    public function getLatestEpisodesAttribute() {
        return $this->episodes()->orderBy('updated_at','desc')->get();
    }

    public function getImageUrlAttribute() {
        $image = $this->images()->first();
        $size =150;

        if ($image) {
            return $image->squareImage($size);
        } else {
            $waterMaker = 'imageView2/1/w/'.$size.'/h/'.$size;
            $baseUrl = config('app.QNImageUrl');
            return $baseUrl.'/'.$this->key.'?'.$waterMaker;
        }
    }

    public function getCoverageUrlAttribute() {
        $image = $this->images()->first();
        if ($image) {
            return $image->imageWH(800,450);
        } else {
            $waterMaker = 'imageView2/1/w/800/h/450';
            $baseUrl = config('app.QNImageUrl');
            return $baseUrl.'/'.$this->key.'?'.$waterMaker;
        }
    }

    public function getDetailImageUrlAttribute()
    {
        $image = $this->images()->first();
        if ($image) {
            return $image->imageWH(300, 400);
        } else {
            $waterMaker = 'imageView2/1/w/800/h/450';
            $baseUrl = config('app.QNImageUrl');
            return $baseUrl . '/' . $this->key . '?' . $waterMaker;
        }
    }

    public function getCoverageSquareAttribute() {
        $image = $this->images()->first();
        if ($image) {
            return $image->imageWH(150,200);
        } else {
            $waterMaker = 'imageView2/1/w/300/h/400';
            $baseUrl = config('app.QNImageUrl');
            return $baseUrl.'/'.$this->key.'?'.$waterMaker;
        }
    }


    public function getCoverageImageAttribute() {
        $image = $this->images()->first();
        if ($image) {
            return $image->imgURL();
        } else {
            $size =150;
            $waterMaker = 'imageView2/1/w/'.$size.'/h/'.$size;
            $baseUrl = config('app.QNImageUrl');
            return $baseUrl.'/'.$this->key.'?'.$waterMaker;
        }
    }

    public function getSiteUrlAttribute () {
        return env('APP_URL').'/show/'.$this->id;
    }

    public function getEztvUrlAttribute () {
        $episode = $this->episodes()->where('url_provider','like','%/shows/%')->first();
        //https://eztv.ag/shows/666/the-soul-man/
        $eztvURL = 'https://eztv.ag';
        if(!is_null($episode)) {
            $eztvURL = $eztvURL.$episode->url_provider;
        }
        return $eztvURL;
    }

    public function getTtmjUrlAttribute () {
        $episode = $this->episodes()->where('url_provider','like','%/meiju/%')->first();
        $eztvURL = 'http://www.ttmeiju.com';
        if(!is_null($episode)) {
            $eztvURL = $eztvURL.$episode->url_provider;
        }
        return $eztvURL;
    }

    public function getYyetsUrlAttribute () {
        $episode = $this->episodes()->where('provider','like','%yyets%')->first();
        $eztvURL = '';
        if(!is_null($episode)) {
            $eztvURL = $episode->url_provider;
        }
        return $eztvURL;
    }

    public function getKeyWordAttribute() {
        return $this->name_zh.'  '.$this->name_en.'百度网盘下载,迅雷下载,种子下载,磁力下载,电驴下载,网盘下载,中英文双语字幕';
    }
    public function getExcerptAttribute() {
        $text = strip_tags($this->detail);
        return str_limit($text,120);
    }
    public function getShowImageAttribute() {

        $image = $this->images()->first();
        $size =150;

        if ($image) {
            return $image->squareImage($size);
        } else {
            
            $waterMaker = 'imageView2/2/w/'.$size.'/h/'.'150';
            $baseUrl = config('app.QNImageUrl');
            return $baseUrl.'/'.$this->key.'?'.$waterMaker;
        }
    }

    public function getLatestEpisodeAttribute () {
        $firstEpisode = $this->episodes()->orderBy('created_at','desc')->first();
        if($firstEpisode) {
            return str_ireplace('[eztv]','',$firstEpisode->raw_name);
        } else {
            return '';
        }
    }
    public function getEpListAttribute () {
        return $this->episodes()->pluck('id')->toArray();

    }

    public function getEpisodesListAttribute () {
        return $this->episodes()->lists('raw_name','id');
    }


    public function getEpAddAttribute () {
        return Episode::Where('raw_name','like','%'.$this->name_zh.'%')->where('show_id','!=',$this->id)->pluck('raw_name','id');
    }


    public function getScoreAttribute() {
        if($this->vote){
            return $this->vote->score;

        }
    }
}
